package com.zzh.rabbit.wifi

import android.Manifest
import android.net.wifi.ScanResult
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.hjq.permissions.XXPermissions
import com.zzh.lib.core.HLibrary
import com.zzh.lib.core.listener.HScanWifiResultCallback
import com.zzh.lib.core.utils.HNetUtils
import com.zzh.lib.core.utils.LogUtils
import com.zzh.rabbit.databinding.ActivityWifiBinding
import com.zzh.rabbit.demo.base.BaseActivity

class WifiActivity : BaseActivity<WifiViewModel, ActivityWifiBinding>() {
    override fun initViewAndData(savedInstanceState: Bundle?) {
        super.initViewAndData(savedInstanceState)
        enableEdgeToEdge()
        ViewCompat.setOnApplyWindowInsetsListener(bind.main) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        HLibrary.getInstance().registerScanWifiReceiver(this, object : HScanWifiResultCallback(){
            override fun onScanWifiResult(scanResults: List<ScanResult?>?) {
                scanResults?.forEach { s ->
                    LogUtils.e("--------扫描到的数据---------- $s")
                }
            }
        })
    }

    override fun onDestroy() {
        HLibrary.getInstance().unregisterScanWifiReceiver(this)
        super.onDestroy()
    }

    override fun setListener() {
        super.setListener()
        bind.btnScanWifi.setOnClickListener {
            XXPermissions.with(this).permission(
                arrayOf(
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
                )
            ).request { list, all ->
                run {
                    if (all) {
                        HNetUtils.scanNearByWifi()
                    }
                }
            }
        }
    }
}